package today.rocky.memory.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import today.rocky.memory.domain.*;
import today.rocky.memory.domain.bo.*;
import today.rocky.memory.domain.dto.MemberDTO;
import today.rocky.memory.exception.BusinessException;
import today.rocky.memory.mapper.FeedBackMapper;
import today.rocky.memory.mapper.InspireMapper;
import today.rocky.memory.mapper.MemberMapper;
import today.rocky.memory.mapper.QuoteMapper;
import today.rocky.memory.service.MemberService;

import java.util.Date;
import java.util.List;

/**
 * @author Rocky
 * @version 1.0
 * @date 2020/5/22 18:54
 * @description
 */
@Slf4j
@Service
public class MemberServiceImpl implements MemberService {

	@Autowired
	private MemberMapper memberMapper;

	@Autowired
	private FeedBackMapper feedBackMapper;

	@Autowired
	private PasswordEncoder passwordEncoder;

	@Autowired
	private InspireMapper inspireMapper;

	@Autowired
	private QuoteMapper quoteMapper;



	@Override
	public int userRegistry(RegistryParam param) throws BusinessException {
		Member member = new Member();
		BeanUtils.copyProperties(param,member);
		member.setPassword(passwordEncoder.encode(param.getPassword()));
		log.info(member.toString());
		MemberExample example = new MemberExample();
		example.createCriteria().andEmailEqualTo(param.getEmail());
		List<Member> members = memberMapper.selectByExample(example);
		if(members.size()>1){
			throw new BusinessException("账号已经存在");
		}
		member.setCreateTime(new Date());
		memberMapper.insert(member);
		return 1;
	}

	@Override
	public Member userLogin(LoginParam param) throws BusinessException {
		MemberExample example = new MemberExample();
		example.createCriteria().andEmailEqualTo(param.getEmail());
		List<Member> members = memberMapper.selectByExample(example);
		if(members==null){
			throw new BusinessException("账号不存在");
		}
		if(members.size()>1){
			throw new BusinessException("账号被重复注册，请联系管理员");
		}
		Member member  = members.get(0);
		if(!passwordEncoder.matches(param.getPassword(),member.getPassword())){
			throw new BusinessException("密码错误请重新登录");
		}
		return member;
	}

	@Override
	public int updateUserIcon(MemberDTO param) {
		MemberExample example = new MemberExample();
		example.createCriteria().andEmailEqualTo(param.getEmail());
		Member member = new Member();
		member.setIcon(param.getIcon());
		return memberMapper.updateByExampleSelective(member, example);
	}

	@Override
	public Member refreshMemberData(RefreshParam param) throws BusinessException {
		MemberExample example = new MemberExample();
		example.createCriteria().andIdEqualTo(param.getId()).andEmailEqualTo(param.getEmail()).andNicknameEqualTo(param.getNickname());
		List<Member> members = memberMapper.selectByExample(example);
		if(members.size()>1){
			throw new BusinessException("账号被重复注册，请联系管理员");
		}
		return members.get(0);
	}

	@Override
	public int saveFeedBack(FeedBackParam param) throws BusinessException {
		int resCode = -1;
		MemberExample example = new MemberExample();
		example.createCriteria().andIdEqualTo(param.getUserId()).andEmailEqualTo(param.getEmail()).andNicknameEqualTo(param.getNickname());
		List<Member> members = memberMapper.selectByExample(example);
		if(members.size() == 0){
			 throw new BusinessException("账号不存在，无法提交反馈");
		}else if(members.size()>1){
			throw new BusinessException("账号被重复注册，请联系管理员");
		}else {
			FeedBack feedBack = new FeedBack();
			BeanUtils.copyProperties(param,feedBack);
			feedBack.setCreateTime(new Date());
			resCode = feedBackMapper.insertSelective(feedBack);
		}
		return resCode;
	}

	@Override
	public int saveInspire(InspireParam param) throws BusinessException {
		int resCode = -1;
		MemberExample example = new MemberExample();
		example.createCriteria().andIdEqualTo(param.getUserId()).andEmailEqualTo(param.getEmail()).andNicknameEqualTo(param.getNickname());
		List<Member> members = memberMapper.selectByExample(example);
		if(members.size() == 0){
			throw new BusinessException("账号不存在，无法提交灵感");
		}else if(members.size()>1){
			throw new BusinessException("账号被重复注册，请联系管理员");
		}else {
			Inspire inspire = new Inspire();
			BeanUtils.copyProperties(param,inspire);
			inspire.setCreateTime(new Date());
			resCode = inspireMapper.insertSelective(inspire);
		}
		return resCode;
	}

	@Override
	public int saveQuote(QuoteParam param) throws BusinessException {
		int resCode = -1;
		MemberExample example = new MemberExample();
		example.createCriteria().andIdEqualTo(param.getUserId()).andEmailEqualTo(param.getEmail()).andNicknameEqualTo(param.getNickname());
		List<Member> members = memberMapper.selectByExample(example);
		if(members.size() == 0){
			throw new BusinessException("账号不存在，无法提交名言");
		}else if(members.size()>1){
			throw new BusinessException("账号被重复注册，请联系管理员");
		}else {
			Quote quote = new Quote();
			BeanUtils.copyProperties(param,quote);
			quote.setCreateTime(new Date());
			resCode = quoteMapper.insertSelective(quote);
		}
		return resCode;
	}
}
